#ifndef QTLISTWIDGET_H
#define QTLISTWIDGET_H

#include <QAbstractScrollArea>
#include <QMap>

class QtListWidgetItem;

#ifdef QtUi
class QTUI_EXPORT QtListWidget : public QAbstractScrollArea {
#else
class QtListWidget : public QAbstractScrollArea {
#endif
    Q_OBJECT

    Q_PROPERTY(int itemHeight READ itemHeight WRITE setItemHeight)
    Q_PROPERTY(QColor background READ backgroundColor WRITE setBackgroundColor)
    Q_PROPERTY(QColor border READ itemBorder WRITE setItemBorder)
    Q_PROPERTY(QColor text READ textColor WRITE setTextColor)
    Q_PROPERTY(QColor subText READ subTextColor WRITE setSubTextColor)
    Q_PROPERTY(QColor itemSelected READ itemSelectedColor WRITE setItemSelectedColor)
    Q_PROPERTY(QColor itemHover READ itemHoverColor WRITE setItemHoverColor)
    Q_PROPERTY(QColor itemColor READ itemColor WRITE setItemColor)

public:
    explicit QtListWidget(QWidget *parent = nullptr);
    ~QtListWidget();

    void clear();
    QtListWidgetItem *currentItem();
    QtListWidgetItem *itemAt(int index);
    void addItem(QtListWidgetItem *_item);
    bool isContains(const QString &txt);

public:
    QColor backgroundColor() const;
    void setBackgroundColor(const QColor &backgroundColor);

    QColor itemBorder() const;
    void setItemBorder(const QColor &itemBorder);

    int itemHeight() const;
    void setItemHeight(int itemHeight);

    QColor textColor() const;
    void setTextColor(const QColor &textColor);

    QColor subTextColor() const;
    void setSubTextColor(const QColor &subTextColor);

    void setIconSize(const QSize &iconSize);

    QColor itemSelectedColor() const;
    void setItemSelectedColor(const QColor &itemSelectedColor);

    QColor itemHoverColor() const;
    void setItemHoverColor(const QColor &itemHoverColor);

    QColor itemColor() const;
    void setItemColor(const QColor &itemColor);

signals:
    void itemClicked(int index);

private slots:
    void adjust();

private:
    void initAttributes();

private:
    int m_firstItem;
    int m_itemHeight;
    int m_currentItem;
    int m_hoverItem;

    int m_marginLeft;
    int m_marginRight;
    int m_marginTop;
    int m_marginBottom;

    QSize m_iconSize;

    QColor m_backgroundColor;
    QColor m_itemBorder;
    QColor m_itemColor;
    QColor m_itemSelectedColor;
    QColor m_itemHoverColor;
    QColor m_textColor;
    QColor m_subTextColor;

    QMap<int, QtListWidgetItem*> m_items;

protected:
    void resizeEvent(QResizeEvent *) override;
    void paintEvent(QPaintEvent *) override;
    virtual void drawItemInfo(QPainter *painter, QtListWidgetItem *_item);

    void mousePressEvent(QMouseEvent *e) override;
    void mouseMoveEvent(QMouseEvent *e) override;
    void leaveEvent(QEvent *e) override;
};

///////////////////////////////////////////////////////////
#ifdef QtUi
class QTUI_EXPORT QtListWidgetItem  {
#else
class QtListWidgetItem {
#endif
    friend class QtListWidget;
public:
    explicit QtListWidgetItem(int _id);
    explicit QtListWidgetItem(int _id, const QString &_txt);
    explicit QtListWidgetItem(int _id, const QString &_txt, const QString &_subTxt);
    explicit QtListWidgetItem(int _id, const QString &_txt, const QString &_subTxt, const QPixmap &icon);
    ~QtListWidgetItem();

    int id();
    void setId(const int &_id);

    QRect rect() const;
    void setRect(const QRect &rect);

    QPixmap icon() const;
    void setIcon(const QPixmap &icon);

    QString text() const;
    void setText(const QString &text);

    QString subText() const;
    void setSubText(const QString &subText);

private:
    int m_id;
    QRect m_rect;
    QString m_text;
    QString m_subText;
    QPixmap m_icon;
};

#endif // QTLISTWIDGET_H
